package com.atguigu.resp;

import lombok.Getter;

import java.util.Arrays;

@Getter
public enum ReturnCodeEnum
{
    /**操作失败**/
    RC999("999","操作XXX失败"),
    /**操作成功**/
    RC200("200","success"),
    /**服务降级**/
    RC201("201","服务开启降级保护,请稍后再试!"),
    /**热点参数限流**/
    RC202("202","热点参数限流,请稍后再试!"),
    /**系统规则不满足**/
    RC203("203","系统规则不满足要求,请稍后再试!"),
    /**授权规则不通过**/
    RC204("204","授权规则不通过,请稍后再试!"),
    RC205("205","新用户注册失败,请稍后再试!"),
    /**access_denied**/
    RC403("403","无访问权限,请联系管理员授予权限"),
    /**access_denied**/
    RC401("401","匿名用户访问无权限资源时的异常"),
    RC404("404","404页面找不到的异常"),
    /**服务异常**/
    RC500("500","系统异常，请稍后重试"),

    RC375("375","数学运算异常，请稍后重试"),

    RC1000("1000","RC1000空指针异常"),
    CLIENT_AUTHENTICATION_FAILED("1001","客户端认证失败"),
    USERNAME_OR_PASSWORD_ERROR("1002","用户名或密码错误"),
    GET_SPRINGSECURITYUSERINFO_ERROR("1003","SpringSecurityUserDetails获取失败"),
    BUSINESS_ERROR("1004","业务逻辑异常"),
    REQPARAM_NEGATIVE("1005","请求参数不能是负数"),
    REQPARAM_ISNULL("1006","请求参数不能是NULL"),
    SAVE_ERROR("1007","保存信息进数据库失败"),
    INVALID_TOKEN("2001","访问令牌不合法"),
    INVALID_CAPTCHA("2002","验证码输入不正确或超时过期"),
    UPLOAD_FILE_FAIL("2003","上传图片服务器失败"),
    ACCESS_DENIED("2004","没有权限访问该资源");


    /**自定义状态码**/
    private final String code;
    /**自定义描述**/
    private final String message;

    ReturnCodeEnum(String code, String message){
        this.code = code;
        this.message = message;
    }

    //遍历枚举V1
    public static ReturnCodeEnum getReturnCodeEnum(String code)
    {
        for (ReturnCodeEnum element : ReturnCodeEnum.values()) {
            if(element.getCode().equalsIgnoreCase(code))
            {
                return element;
            }
        }
        return null;
    }

    //遍历枚举V2
    public static ReturnCodeEnum getReturnCodeEnumV2(String code)
    {
        return Arrays.stream(ReturnCodeEnum.values()).filter(x -> x.getCode().equalsIgnoreCase(code)).findFirst().orElse(null);
    }

    /*public static void main(String[] args)
    {
        System.out.println(getReturnCodeEnumV2("200"));
        System.out.println(getReturnCodeEnumV2("200").getCode());
        System.out.println(getReturnCodeEnumV2("200").getMessage());
    }*/
}

